Skip to content

Fix UFuncTypeError for integer input coordinates (#300)#312

Open
gaoflow wants to merge 1 commit into
GeoStat-Framework:mainfrom
gaoflow:fix/300-integer-coords-anisotropy
Open

Fix UFuncTypeError for integer input coordinates (#300)#312
gaoflow wants to merge 1 commit into
GeoStat-Framework:mainfrom
gaoflow:fix/300-integer-coords-anisotropy

Conversation

@gaoflow
Copy link
Copy Markdown

@gaoflow gaoflow commented Jun 1, 2026

Closes #300

Problem

core._adjust_for_anisotropy subtracts the (float) center from the input coordinates in place:

X -= center

When the coordinates are integers — e.g. OrdinaryKriging(x, y, z) with integer x/y, or integer query points passed to execute("points", ...) — NumPy (≥ 2) refuses the in-place subtraction under the same_kind casting rule:

numpy._core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'subtract'
output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

Reproduction (current main, NumPy 2.4)

import numpy as np
from pykrige.ok import OrdinaryKriging

x = np.array([10, 20, 30, 40, 50])   # integer coordinates
y = np.array([10, 25, 35, 45, 60])
z = np.array([1.1, 2.2, 0.5, 3.1, 1.7])
OK = OrdinaryKriging(x, y, z, variogram_model="linear")
OK.execute("points", np.array([15, 25]), np.array([15, 25]))   # -> UFuncTypeError

Fix

Cast X to float64 at the start of _adjust_for_anisotropy. The docstring already documents X as a float array, so this simply enforces that contract. All Ordinary/Universal Kriging classes (2D and 3D) route their coordinate handling through this single function, so the one-line fix covers every variant.

Verification

  • Integer-coordinate kriging now returns results identical to the equivalent float-coordinate run (2D and 3D), confirmed numerically.
  • Added test_core_adjust_for_anisotropy_integer_coords (unit-level + an end-to-end OrdinaryKriging run); it fails on main with the UFuncTypeError and passes with this change.
  • Full tests/ suite: 54 passed, 2 skipped (no new failures). black / isort clean.

…k#300)

_adjust_for_anisotropy subtracts the (float) center from the input
coordinates in place via 'X -= center'. When the coordinates are given as
an integer array (e.g. OrdinaryKriging(x, y, ...) with integer x/y, or
integer query points in execute()), NumPy refuses the in-place subtraction
under the 'same_kind' casting rule and raises:

    numpy._core._exceptions._UFuncOutputCastingError: Cannot cast ufunc
    'subtract' output from dtype('float64') to dtype('int64') ...

Cast X to float64 at the start of the function (the docstring already
documents X as a float array). This fixes ordinary and universal kriging
in 2D and 3D, since they all route coordinate handling through this
function. Adds a regression test.

Closes GeoStat-Framework#300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant